import http from '../utils/httpRequest'

import { Message } from 'element-ui'

export function baseAjax (url, data, type, successHandle, failHandle, finallyHandle, responseType) {
  let params = {
    url: http.adornUrl(url),
    method: type
  }
  if (type === 'get') {
    params.params = http.adornParams(data)
  } else {
    params.data = http.adornData(data)
  }
  if (responseType) {
    params.responseType = responseType
  }
  http(params).then(({data}) => {
    if (data && data.code === 0) {
      successHandle(data)
    } else if (data.code === 600) {
      if (!failHandle) {
      } else {
        failHandle(data)
      }
    } else {
      let message = data.msg ? data.msg : data.message
      Message.error(message)
      if (!failHandle) {
      } else {
        failHandle(data)
      }
    }
  }).finally(() => {
    if (finallyHandle) {
      finallyHandle()
    }
  }).catch(err => console.log('err', err))
}
